const redis = require("../utils/redis")
const returnOdd = 0.7; //总返奖率
const freeOdd = 0.1
const _ = require("lodash")
const JACKPOT_LV = 0.05

//3 4 5 连自然概率出现的关系
// 3连变成 4连 两端需要1/12的概率 ，中间则为 1/6 整体概率为 5/36 的概率
// 4变5的概率为 3/36分之一  分成36分，则 3：4：5 = 432 ： 60 ： 5
const serviesMultiplier = [432, 60, 5]
// const serviesMultiplier = [1000, 100, 5]

//计算实际概率 通过calImageLv 方法获得，这是固定值，计算好，避免重复值
const multiplierSuccessLines = [
    { image: 1, multiplier: 60, count: 3, lv: 0.0007054884936242022 },
    { image: 1, multiplier: 150, count: 4, lv: 0.00009798451300336142 },
    { image: 1, multiplier: 600, count: 5, lv: 0.000008165376083613452 },
    { image: 2, multiplier: 50, count: 3, lv: 0.0010582327404363033 },
    { image: 2, multiplier: 100, count: 4, lv: 0.00014697676950504212 },
    { image: 2, multiplier: 300, count: 5, lv: 0.000012248064125420177 },
    { image: 3, multiplier: 40, count: 3, lv: 0.0014109769872484043 },
    { image: 3, multiplier: 80, count: 4, lv: 0.00019596902600672284 },
    { image: 3, multiplier: 200, count: 5, lv: 0.000016330752167226904 },
    { image: 4, multiplier: 30, count: 3, lv: 0.0017637212340605053 },
    { image: 4, multiplier: 60, count: 4, lv: 0.0002449612825084035 },
    { image: 4, multiplier: 150, count: 5, lv: 0.00002041344020903363 },
    { image: 5, multiplier: 25, count: 3, lv: 0.0021164654808726066 },
    { image: 5, multiplier: 50, count: 4, lv: 0.00029395353901008424 },
    { image: 5, multiplier: 100, count: 5, lv: 0.000024496128250840355 },
    { image: 6, multiplier: 20, count: 3, lv: 0.002469209727684708 },
    { image: 6, multiplier: 40, count: 4, lv: 0.00034294579551176496 },
    { image: 6, multiplier: 80, count: 5, lv: 0.000028578816292647077 },
    { image: 7, multiplier: 15, count: 3, lv: 0.0028219539744968086 },
    { image: 7, multiplier: 30, count: 4, lv: 0.0003919380520134456 },
    { image: 7, multiplier: 60, count: 5, lv: 0.0000326615043344538 },
    { image: 8, multiplier: 10, count: 3, lv: 0.00317469822130891 },
    { image: 8, multiplier: 20, count: 4, lv: 0.00044093030851512634 },
    { image: 8, multiplier: 40, count: 5, lv: 0.00003674419237626053 },
    { image: 9, multiplier: 5, count: 3, lv: 0.0035274424681210107 },
    { image: 9, multiplier: 10, count: 4, lv: 0.000489922565016807 },
    { image: 9, multiplier: 20, count: 5, lv: 0.00004082688041806726 },
    { image: 1000, multiplier: 100, count: 3, lv: 0.0003527442468121011 },
    { image: 1000, multiplier: 500, count: 4, lv: 0.00004899225650168071 },
    {
      image: 1000,
      multiplier: 1000,
      count: 5,
      lv: 0.000004082688041806726
    },
    { image: 11, multiplier: 0, count: 3, lv: 0 },
    { image: 11, multiplier: 0, count: 4, lv: 0 },
    { image: 11, multiplier: 0, count: 5, lv: 0 },
    { image: 12, multiplier: 0, count: 3, lv: 0.012093726379440665 },
    { image: 12, multiplier: 0, count: 4, lv: 0.0016796842193667592 },
    { image: 12, multiplier: 0, count: 5, lv: 0.00013997368494722993 }
  ]
  
const imageIds = [1,2,3,4,5,6,7,8,9,10,1000,12]

const FREE_GAME = {};

class NinelineModel{
    constructor(){
        this.gameId = 1001;
    }
    /**
     * 增加房间库存
     */
    async addGameInventory(gems){
        try{
            await redis.hincrbyfloat("game_inventory", this.gameId, gems)
        }catch(err){
            throw err;
        }
    }
    async addJackopt(gems){
        try{
            await redis.hincrbyfloat("game_jackpot", this.gameId, gems)
        }catch(err){
            throw err;
        }
    }
    async getJackpot(){
        try{
            let score = await redis.hget("game_jackpot", this.gameId)
            return +score || 0;
        }catch(err){
            throw err;
        }
    }
    async addPlayerBetAndWins(userId, betGems, wins){
        try{
            await redis.hincrbyfloat("game_user_bet_"+this.gameId, userId, betGems)
            await redis.hincrbyfloat("game_all_bet", this.gameId, betGems)
            await redis.hincrbyfloat("game_user_wins_"+this.gameId, userId, wins)
            await redis.hincrbyfloat("game_all_wins", this.gameId, wins)
        }catch(err){
            throw err;
        }
    }
    async getPlayerAllBet(){
        try{
            let gems = await redis.hget("game_all_bet", this.gameId)
            return +gems;
        }catch(err){
            throw err;
        }
    }
    async getUserAllBetAndTotalWins(userId){
        try{
            let totalBet = await redis.hget("game_user_bet_"+this.gameId, userId)
            let totalWin = await redis.hget("game_user_wins_"+this.gameId, userId)
            let platTotalBet = await redis.hget("game_all_bet",this.gameId)
            let platTotalWin = await redis.hget("game_inventory",this.gameId)
            return {
                totalBet :+totalBet || 0,
                totalWin : +totalWin|| 0,
                platTotalBet: +platTotalBet || 0,
                platTotalWin: +platTotalWin || 0
            };
        }catch(err){
            throw err;
        }
    }
    async clearUserData(userId){
        try{    
            console.log("userId:"+userId)
            await redis.hdel("game_user_bet_"+this.gameId, userId)
            await redis.hdel("game_user_wins_"+this.gameId, userId)
        }catch(err){
            throw err;
        }
    }
    async clearSystemData(){
        await redis.hdel("game_all_bet",this.gameId)
        await redis.hdel("game_inventory",this.gameId)
    }
    async addPlayerFreeCount(userId, freeCount){
        try{
            FREE_GAME.userId = FREE_GAME.userId || 0;
            FREE_GAME.userId += freeCount;
            return;
            await redis.hincrby("game_free_"+this.gameId, userId, freeCount)
        }catch(err){
            throw err;
        }
    }
    async getPlayerFreeCount(userId){
        try{
            return FREE_GAME.userId || 0;
            let freeCount = await redis.hget("game_free_"+this.gameId, userId)
            return +freeCount || 0;
        }catch(err){
            throw err;
        }
    }
    async staticPlayerGame(userId, betGems, wins){
        try{
            let jackpotGems = +(wins * 0.05).toFixed(2);
            let playerWins = wins - jackpotGems;
            let systemWins = betGems - wins;
            await this.addGameInventory(systemWins);
            await this.addJackopt(jackpotGems);
            await this.addPlayerBetAndWins(userId, betGems, playerWins)
        }catch(err){
            throw err;
        }
    }
    async calPlayerY(){
        try{

        }catch(err){
            throw err;
        }
    }
    async begin(userId, betAmount){
        try{

        }catch(err){
            throw err;
        }
    }
    /**
     * @param {*} userId 
     * @param {*} betAmount 
     */
    async startGame(userId, betAmount){
        try{
            let difen = betAmount/9
            let lineResults = await this.calcBetResult(1);  //获得奖励
            let gameFreeFlag = false;
            let freeCount = await this.getPlayerFreeCount(userId);
            if(freeCount>0){
                gameFreeFlag = true;
                // console.log("免费游戏")
                await this.addPlayerFreeCount(userId, -1)
            }
            let [views, winLineResults] = this.fillWinViews(lineResults, 100000)
            await this.handlerJackpot(views, winLineResults)
            let defaultImageOdds = [100,100,100,100,100,100,100,100,100,100,100,100]
            let imageOdds = [100,100,100,100,100,100,100,100,100,100,100,100];
            //填充没有中奖的部分
            this.fillNotWinImage(views, winLineResults, imageOdds)
            this.fillAllImage(views, imageOdds)
            let playerFreeGameCount = await this.handlerFreeGame(userId, winLineResults);  //处理免费游戏情况
            //计算玩家总盈利
            let totalWinMultiplier = 0, jackpotScore = 0, lineScore = 0;
            for(let {multiplier,image,score} of winLineResults){
                if(image!=1000){
                    totalWinMultiplier += multiplier;
                }else{
                    jackpotScore += score || 0;
                }
            }
            lineScore = totalWinMultiplier * difen;
            let platWinScore = +((gameFreeFlag ? 0: betAmount) - lineScore).toFixed(2)
            //百分之二贡献jackpot
            // let conJackpotScore = +(lineScore* 0.02.toFixed(2))
            let conJackpotScore = 0
            let taxiLineScore = +lineScore
            await this.addGameInventory(platWinScore)
            await this.addPlayerBetAndWins(userId, gameFreeFlag ?0: betAmount, taxiLineScore+ jackpotScore)
            return {
                views, 
                result : {
                    freeCount: freeCount + playerFreeGameCount-1 <=0 ? 0 : freeCount + playerFreeGameCount-1,
                    free: gameFreeFlag,
                    wins: {
                        lineScore,
                        taxiLineScore,
                        jackpotScore:0,
                    },
                    winLines: winLineResults,
                },
            }
        }catch(err){
            throw err;
        }
    }
    /**
     * 处理游戏免费情况
     * @param {*} winLineResult 
     */
    async handlerFreeGame(userId, winLineResults){
        try{
            //免费游戏
            let playerFreeGameCount = 0;
            for(let winLineResult of winLineResults){
                if(winLineResult.image == 12){
                    winLineResult.multiplier = 0;
                    if(winLineResult.count == 3){
                        playerFreeGameCount +=1;
                    }
                    if(winLineResult.count == 4){
                        playerFreeGameCount +=3;
                    }
                    if(winLineResult.count == 5){
                        playerFreeGameCount += 5;
                    }
                }
            }
            //增加免费次数
            if(playerFreeGameCount > 0){
                // console.log("----------免费游戏次数增加:"+playerFreeGameCount)
                await this.addPlayerFreeCount(userId, playerFreeGameCount)
            }
            return playerFreeGameCount
        }catch(err){
            throw err;
        }
    }

    async handlerJackpot(views, winLineResuls){
        try{
            let jackpotRandomNumber = Math.random();
            let jackpotCount = 0,winJackpotLv = 0;
            // jackpotRandomNumber = 0.000001
            if(jackpotRandomNumber < JACKPOT_LV) {
                let totalNumber = serviesMultiplier.reduce((prev, cur) => prev+cur);
                let randNumber = Math.random() * totalNumber;
                for(let i = serviesMultiplier.length-1; i>=0; i--){
                    if(randNumber < serviesMultiplier[i]){
                        if(i == 0){
                            winJackpotLv =0.1
                            jackpotCount = 3;
                        }
                        if(i == 2){
                            winJackpotLv =0.2
                            jackpotCount = 4;
                        }
                        if(i == 3){
                            winJackpotLv =0.3
                            jackpotCount = 5;
                        }
                        let line = Math.ceil(Math.random()*9);
                        let lineResult = this.findWinLinePosition(views, {line, image:1000, count:jackpotCount, multiplier:0})
                        if(!lineResult[0]){
                            let ret = this.pushPlayerWinsLine(views, winLineResuls, {line:line, image:1000, count:jackpotCount, multiplier:0,left:lineResult[1]},0,100000000)
                            if(!ret[0]){
                                let jackpotScore = await this.getJackpot();
                                let winLine = ret[3]
                                let playerWinJackpot = +(jackpotScore* winJackpotLv.toFixed(2));
                                await this.addJackopt(-playerWinJackpot);
                                winLine.score = playerWinJackpot;
                            }

                        }
                        break;
                    }
                }
            }
        }catch(err){
            throw err;
        }
    }

    /**
     * 填充没有中奖的部分
     * 1，中奖的线前后不能填充钻石
     * 2，没有中奖的填充不能中奖，如果中奖，则用默认概率填充
     * 3，如果一条线10次都填充不好不能中奖，则重新摇奖
     * @param {*} views 
     * @param {*} winLineResuls 
     */
    async fillNotWinImage(views, winLineResuls, imageOdds){
        try{
            let lineRules = this.getLineRule();
            for(let {line, image, count, left} of winLineResuls){
                let linePositions = lineRules.find((item)=> item.index == line).positions;
                let leftIndex = left -1;  
                let rightIndex = left+count;

                let leftLinePosition = linePositions[leftIndex]
                let rightLinePosition = linePositions[rightIndex]
                if(leftIndex>=0 && views[leftLinePosition[0]][leftLinePosition[1]]==-1){
                    this.fillPositionImage(views, leftLinePosition, imageOdds, [image,10])
                }
                if(rightIndex<5 && views[rightLinePosition[0]][rightLinePosition[1]]==-1){
                    this.fillPositionImage(views, rightLinePosition, imageOdds, [image,10])
                }
            }
            return views;
        }catch(err){
            throw err;
        }
    }

    /**
     * 填充剩余部分
     * @param {*} views 
     */
    async fillAllImage(views, imageOdds){
        try{
            let maxSeriesNumber = 3;
            let lineRules = this.getLineRule();
            for(let i = 0; i< lineRules.length; i++){
                let {positions, index} = lineRules[i]
                //找到第一个 image -1的 图，往后推
                let firstIndex = _.findIndex(positions, ([x,y] = item) => views[x][y] == -1);
                //如果没有-1的，则看下一个连线
                if(firstIndex ==-1){
                    continue;
                }
                //有-1的情况，
                //如果第一个-1的下标大于0，则往左找
                if(firstIndex>0){
                    let seriesImage = views[positions[firstIndex-1][0]][positions[firstIndex-1][1]]
                    let currSeriesNumber = 1;
                    for(let i = firstIndex-2; i>=0; i--){
                        if(views[positions[i][0]][positions[i][1]] == seriesImage){
                            currSeriesNumber ++;
                        }else{
                            break;
                        }
                    }
                    let notImage = [];
                    if(currSeriesNumber >=maxSeriesNumber-1){
                        notImage = [seriesImage]
                    }
                    // console.log("填充", JSON.stringify(positions[firstIndex]))
                    this.fillPositionImage(views, positions[firstIndex], imageOdds, notImage)
                    i --;
                }else if(firstIndex == 0){
                    // console.log("填充", JSON.stringify(positions[firstIndex]))
                    this.fillPositionImage(views, positions[firstIndex], imageOdds, [])
                    i --;
                }
            }
        }catch(err){
            throw err;
        }
    }

    /**
     * 填充一个位置
     * @param {*} views 
     * @param {*} position 
     * @param {*} imageOddLvs 
     * @param {*} notImages  不能为这些图片
     */
    fillPositionImage(views, position, imageOdds, notImages){
        try{
            // console.log(imageOdds)
            for(let i = 0; i< imageOdds.length;i++){
                if(notImages.includes(multiplierSuccessLines[i].image)){
                    imageOdds[i]= 0;
                }
            }
            let oddLvs = [],currMaxLv = 0;
            for(let i = 0; i< imageOdds.length; i++){
                oddLvs.push([currMaxLv, currMaxLv+imageOdds[i]])
                currMaxLv += imageOdds[i]
            }     
            let totalPotLv = imageOdds.reduce((prev, cur) => prev+cur);
            let mathRandnumber = Math.random() * totalPotLv;
            let succcessImage = 0;
            for(let i = 0; i< oddLvs.length; i++){
                let [min,max] = oddLvs[i]
                if(mathRandnumber>=min && mathRandnumber<= max){
                    succcessImage = imageIds[i];
                }
            }
            views[position[0]][position[1]] = succcessImage;
        }catch(err){
            throw err;
        }
    }

    //获取连线规则
    getLineRule(){
        return [
            {positions:[[1,0],[1,1],[1,2],[1,3],[1,4]],index:1}, //L1
            {positions:[[0,0],[0,1],[0,2],[0,3],[0,4]],index:2}, //L2
            {positions:[[2,0],[2,1],[2,2],[2,3],[2,4]],index:3}, //L3
            {positions:[[0,0],[1,1],[2,2],[1,3],[0,4]],index:4}, //L4
            {positions:[[2,0],[1,1],[0,2],[1,3],[2,4]],index:5}, //L5
            {positions:[[1,0],[0,1],[0,2],[0,3],[1,4]],index:6}, //L6
            {positions:[[1,0],[2,1],[2,2],[2,3],[1,4]],index:7}, //L7
            {positions:[[0,0],[0,1],[1,2],[2,3],[2,4]],index:8}, //L8
            {positions:[[2,0],[2,1],[1,2],[0,3],[0,4]],index:9}, //L9
        ]
    }

    /**
     * 检查这个positionIndex 在 这条线上是否存在其他图片了，如果存在，返回true
     * @param {*} views 
     * @param {*} linePositions 
     * @param {*} positionIndex 
     * @param {*} image 
     */
    checkPositionImage(views, linePositions, positionIndex, image){
        try{
            let positions = linePositions[positionIndex];
            let targetImage = views[positions[0]][positions[1]]
            if(targetImage != image && targetImage!=-1){
                return true;
            }
            return false;
        }catch(err){
            throw err;
        }
    }

    /**
     * 给生成中间的一条线找到合适的位置，优先中奖的线，如果找不到，这条线忽略
     * @param {*} views 
     * @param {*} lineResult 
     */
    findWinLinePosition(views, lineResult){
        try{
            let lineRules = this.getLineRule();
            let {line, image, count, multiplier} = lineResult;
            let linePositions = lineRules.find((item) => item.index == line);
            let marginList = [];
            if(count ==2){
                marginList  = [0,3]
            }
            if(count ==3){
                marginList  = [0,2]
            }
            if(count ==4){
                marginList  = [0,1]
            }
            let left = marginList[Math.floor(Math.random()* marginList.length)]
            let occFlag = false;
            //两端不能共线
            let leftIndex = left-1, rightIndex = left+count;
            if(leftIndex >= 0){
                if(this.checkPositionImage(views, linePositions.positions, leftIndex, image)){
                    occFlag = true;
                }
            }
            if(rightIndex < 5){
                if(this.checkPositionImage(views, linePositions.positions, rightIndex, image)){
                    occFlag = true;
                }
            }
            //再检查本身有没有被占用, 比如两条线中间位置公用，如果中间位置不是同一图片，表示占线
            for(let i = left; i<left+count; i++){
                if(this.checkPositionImage(views, linePositions.positions, i, image)){
                    occFlag = true;
                }
            }
            return [occFlag, left];
        }catch(err){
            throw err;
        }
    }

    /**
     * 
     * @param {*} views 
     * @param {*} winLineResuls 
     * @param {*} param2 
     * @param {*} param2 
     */
    pushPlayerWinsLine(views, winLineResuls, {line, image, count, multiplier, left} = lineResult, currMultiplier, maxMultiplier){
        try{
            //超过最大倍数了
            if(currMultiplier + multiplier > maxMultiplier){
                return [true, currMultiplier,  winLineResuls, false]
            }
            let lineRules = this.getLineRule();
            let linePositions = lineRules.find((item) => item.index == line).positions
            for(let i = left; i<left+count; i++){
                let position = linePositions[i];
                views[position[0]][position[1]] = image;
            }
            let winline = {line, image, count, multiplier, left} 
            winLineResuls.push(winline);
            currMultiplier += multiplier;
            return [false, currMultiplier, winLineResuls, winline]
        }catch(err){
            throw err;
        }
    }

    /**
     * 根据奖励来填充奖励视图
     * @param {*} lineResults 
     * @param {*} maxMultiplier  玩家最大可盈，只能填充到最大可盈
     */
    fillWinViews(lineResults, maxMultiplier){
        try{
            let currMultiplier = 0;
            let allLineIndexs = [1,2,3,4,5,6,7,8,9]
            let views = [[-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1]]
            let winLineResuls = [];
            for(let {line, image, count, multiplier} of lineResults){
                let [occFlag,left] = this.findWinLinePosition(views, {line, image, count, multiplier});
                //占线了,再找别的可以用的线
                let overFlag = false;
                if(occFlag){
                    allLineIndexs = _.shuffle(allLineIndexs); //占线了，将所有线打乱, 找到一个不占线的
                    for(let i = 0; i< allLineIndexs.length; i++){
                        let lineIndex = allLineIndexs[i]
                        // console.log("lineIndex:"+lineIndex)
                        let result = this.findWinLinePosition(views, {line:lineIndex, image, count, multiplier});
                        occFlag = result[0];
                        left = result[1]
                        // [occFlag,left]  = result;
                        if(!occFlag){
                            allLineIndexs.splice(i, 1);
                            // console.log("------------进来2")
                           let result = this.pushPlayerWinsLine(views, winLineResuls, {line:lineIndex, image, count, multiplier,left},currMultiplier,maxMultiplier)
                           overFlag = result[0];
                           currMultiplier = result[1];
                           winLineResuls = result[2]
                            if(overFlag){
                                return [views, winLineResuls]
                            }
                            break;
                        }
                    }
                }else{
                    let lineIndex = allLineIndexs.indexOf(line)
                    allLineIndexs.splice(lineIndex, 1);
                    // console.log("---------------进来-----------------")
                    let result = this.pushPlayerWinsLine(views, winLineResuls, {line, image, count, multiplier,left},currMultiplier,maxMultiplier)
                    overFlag = result[0];
                    currMultiplier = result[1];
                    winLineResuls = result[2]
                    if(overFlag){
                        return [views, winLineResuls]
                    }
                }
            }
            return [views, winLineResuls]
        }catch(err){
            throw err;
        }
    }

    /**
     * 根据玩家Y值计算玩家中奖
     * 9条线，给9次机会，如果一次机会
     * @param {*} playerGameY 
     */
    async calcBetResult(playerGameY){
        try{
            let playerWinLines  = []; //玩家成功连线列表， line 几线， image 图片 count 几个 multiple 赔率
            for(let i = 1; i<= 9; i++){
                for(let j = 0; j< multiplierSuccessLines.length; j++){  //每个连线都有一次机会
                    let randNumber = Math.random();
                    if(randNumber < multiplierSuccessLines[j].lv * playerGameY){
                        playerWinLines.push({
                            line:i,
                            image: multiplierSuccessLines[j].image,
                            count: multiplierSuccessLines[j].count,
                            multiplier:multiplierSuccessLines[j].multiplier
                        })
                    }
                }   
            }
            return playerWinLines
        }catch(err){
            throw err;
        }
    }

    
    /**
     * 计算各个图片出现的概率，计算完替换multiplierLines，用计算好的lv来算玩家中奖
     */
    calImageLv(){
        try{
            //玩家奖金分布由后台配置读取, 每个图片对应一个，3叶草和jackpot除外
            let serverBounsDistribution = [1, 1.5 ,2 ,2.5 ,3 ,3.5 ,4, 4.5 ,5, 0.5]
            let bounsDistribution = [];
            for(let serverBoun of serverBounsDistribution){
                bounsDistribution = bounsDistribution.concat([serverBoun,serverBoun, serverBoun])
            }
            // 这个权值如下：
            // 3连在中间变成4连需要 1/6分之一的机会  3连2测 需要1/12 *2的机会， 3连4连出现次数比为 3:1
            //4连变成 5连需要 1/12的机会，但是4连和5连的位置个数比为 2：1 实际 4连和5连出现的次数比为 6：1
            // 又由于 赔率 1：2：4的关系  对招 1：3：18 实际potLineLvs 3 4 5 连的关系为 1: 1.5: 4.5
            let potLineLvs = [];
            for(let i = 0; i< serverBounsDistribution.length*3; i++){
                if(i % 3 ==0){
                    potLineLvs[i] = serviesMultiplier[0]
                }
                if(i % 3 ==1){
                    potLineLvs[i] = serviesMultiplier[1]
                }
                if(i % 3 ==2){
                    potLineLvs[i] = serviesMultiplier[2]
                }
                potLineLvs[i] *= bounsDistribution[i]
            }
            let totalPotLv = potLineLvs.reduce((prev, cur) => prev+cur);
            for(let i = 0; i < multiplierSuccessLines.length; i++){
                multiplierSuccessLines[i].lv = potLineLvs[i]/totalPotLv/multiplierSuccessLines[i].multiplier *returnOdd
                if(i==30){
                    console.log(potLineLvs[i], totalPotLv, multiplierSuccessLines[i].multiplier)
                    console.log(multiplierSuccessLines[i])
                }
            }
            //由于每条线，中5了，实际也是中3的，那么中3连的概率应该加上4连 5连的
            // for(let i = 0; i< multiplierLines.length; i+=3){
            //     for(let j = i; j<i+3; j++){
            //         let nexTotal = 0;
            //         for(let t = 0; t<= i+2-j; t++){
            //             nexTotal += multiplierLines[j+t].lv
            //         }
            //         multiplierLines[j].lv = nexTotal;
            //     }
            // }
            console.log(multiplierSuccessLines)
        }catch(err){
            throw err;
        }
    }

    /**
     * 计算各个图片出现的概率，计算完替换multiplierLines，用计算好的lv来算玩家中奖
     */
     calImageLv(){
        try{
            //玩家奖金分布由后台配置读取, 每个图片对应一个，3叶草和jackpot除外
            let serverBounsDistribution = [1, 1.5 ,2 ,2.5 ,3 ,3.5 ,4, 4.5 ,5, 0.5]
            let bounsDistribution = [];
            for(let serverBoun of serverBounsDistribution){
                bounsDistribution = bounsDistribution.concat([serverBoun, serverBoun, serverBoun])
            }
           
            let potLineLvs = [];
            for(let i = 0; i< bounsDistribution.length; i++){
                potLineLvs[i] = bounsDistribution[i]* serviesMultiplier[i%3] * multiplierSuccessLines[i].multiplier
            }
            let totalPotLv = potLineLvs.reduce((prev, cur) => prev+cur);
            for(let i = 0; i < multiplierSuccessLines.length; i++){
                multiplierSuccessLines[i].lv = potLineLvs[i]/totalPotLv/multiplierSuccessLines[i].multiplier * (returnOdd - freeOdd)
            }
            //免费游戏计算，一次免费游戏相当于白送一个本金，相当于9倍返奖，  3次 27倍 5次 45倍  总共   
            //考虑免费游戏 再转 最终返奖70%的情况，/0.55经过千万数据跑得到的结果
            //serviesMultiplier 是出现的概率，下面还得乘以份额，得到具体的数学概率
            let freePotOdds =  _.clone(serviesMultiplier);
            let equalDivision = [9, 18, 27]
            for(let i =0; i< freePotOdds.length; i++){
                freePotOdds[i] *= equalDivision[i];
            }
            let freetTotalPotLv = freePotOdds.reduce((prev, cur) => prev+cur);
            multiplierSuccessLines[30].lv =  freePotOdds[0]/freetTotalPotLv/ (9*returnOdd) * freeOdd  //(9*(returnOdd - freeOdd)) 9倍返奖，但是实际返奖只有0.6，所以还要*0.6
            multiplierSuccessLines[31].lv =  freePotOdds[1]/freetTotalPotLv/ (18*returnOdd)  * freeOdd 
            multiplierSuccessLines[32].lv =  freePotOdds[2]/freetTotalPotLv/ (27*returnOdd)  * freeOdd 
            console.log(multiplierSuccessLines)
        }catch(err){
            throw err;
        }
    }
}

module.exports = NinelineModel